//
//  WalletProtocol.swift
//  XHWalletUtils
//
//  Created by xinph on 2020/5/16.
//  Copyright © 2020 xinph. All rights reserved.
//

import Foundation
@_exported import SPNetwork
@_exported import RxSwift
@_exported import coswift

public protocol BIP32 {
    
    /// 根据助记词/seed推导出对应某个子账户下的私钥
    /// https://learnblockchain.cn/2018/09/28/hdwallet
    /// - Parameters:
    ///   - password: keystore密码
    ///   - address: 对应账号地址
    func UNSAFE_getPrivateAndPublicKey(password: String, address: String) -> coswift.Promise<(pub: String, pri: String)>
}

/// BTC相关的创建会很耗时
public protocol BIP39 {
    /// 助记词创建账户
    /// https://learnblockchain.cn/2018/09/28/hdwallet
    /// - Parameters:
    ///   - password: 钱包密码，keystore密码
    ///   - name: 钱包名称
    static func createAccountByMnemonics(_ password: String, name: String) -> coswift.Promise<WalletType>

    /// 通过助记词导入账户
    /// - Parameters:
    ///   - password: 钱包密码，keystore密码
    ///   - name: 钱包名称
    ///   - mnemonics: 助记词
    ///   - saveToLocal: 是否保存
    static func importAccountByMnemonics(_ password: String, name: String, mnemonics: String, saveToLocal: Bool) -> coswift.Promise<WalletType>
    
    /// 通过助记词导入账户
    /// - Parameters:
    ///   - path: 推导路径
    ///   - password: keystore密码
    ///   - name: 钱包名称
    ///   - mnemonics: 助记词
    static func importWalletFromMne(_ path: String?, password: String, name: String?, mnemonics: String) -> coswift.Promise<Wallet>
}

/// BTC相关的创建会很耗时
public protocol BIP44 {
    /// 基于当前账户路径下，创建一个新的子账户
    /// https://learnblockchain.cn/2018/09/28/hdwallet
    /// 比如以太坊 m/44’/60’/0’/0 -> m/44’/60’/0’/1
    /// - Parameter password: keystore密码
    /// - Parameter name 账户名称，默认 'Account1'、'Account2'...
    /// - Paramter write 是否写入缓存跟磁盘，默认写入
    /// - closure 结果回执
    func createNewAccount(_ password: String, name: String, cache write: Bool, closure: @escaping (Result<Account>) -> Void)
    
    /// 根据推导路径生成钱包
    /// - Parameters:
    ///   - path: 推导路径
    ///   - password: 助记词密码
    ///   - name: 钱包名称
    ///   - mnemonics: 助记词
    static func createWallet(_ path: String?, password: String, name: String, mnemonics: String) -> coswift.Promise<Wallet>
    
    /// 基于当前账户下的路径，创建n个子账号
    /// - Parameters:
    ///   - password: keystore密码
    ///   - count: 需要创建的数量
    ///   - write 是否写入缓存跟磁盘，默认不写入
    func createSevralAccounts(_ password: String, need count: UInt64, cache write: Bool)
}

public protocol AccountProtocol {
    
    /// 私钥创建账户
    /// - Parameters:
    ///   - password: 钱包密码，keystore密码
    ///   - name: 钱包名称
    static func createAccountByPrivate(_ password: String, name: String) -> coswift.Promise<WalletType>

    /// 通过私钥导入账户
    /// - Parameters:
    ///   - password: 钱包密码，keystore密码
    ///   - name: 钱包名称
    ///   - private: 私钥
    static func importAccountByPrivate(_ password: String, name: String, private: String) -> coswift.Promise<WalletType>
}

public protocol BalanceProtocol {

    /// 获取某个地址下的以太坊余额
    /// - Parameters:
    ///   - address: 目标地址
    ///   - closure: 余额回执闭包
    func getETHBalance(_ address: AddressConvertible, closure: @escaping (Result<String>) -> Void)
}
